﻿using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using TrafficArchives.Common;

namespace TrafficArchives.DataAccess
{
    /// <summary>
    /// 单位数据库操作。
    /// </summary>
    public class UnitDb : DbBase
    {
        /// <summary>
        /// 读取单位数据。
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override PaginationData<DataTable> GetData(Request request)
        {
            var paginationData = new PaginationData<DataTable>();
            var normalSql = "select t.*, rownum as rowno from DWGLTABLE t where " + request.ToSql();
            var sql = request.Pagination.Enabled
                          ? string.Format("select * from ({0}) A where A.rowno > {1} and A.rowno <= {2}", normalSql,
                                          GetPageStart(request.Pagination), GetPageEnd(request.Pagination))
                          : normalSql;
            paginationData.Data = ExecuteDataTable(sql);
            if (request.Pagination.Enabled)
            {
                paginationData.RecordCount = ExecuteCount(normalSql.Replace("t.*, rownum as rowno", "count(*)"));
            }
            return paginationData;
        }

        /// <summary>
        /// 根据Id取单位数据。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override DataTable GetData(string id)
        {
            var request = new Request();
            request.Items.Add(new Condition
            {
                FieldName = "DWGL_ID",
                Operator = ConditionOperator.Equal,
                Values = new object[] { id }
            });
            return GetData(request).Data;
        }

        /// <summary>
        /// 取表结构。
        /// </summary>
        /// <returns></returns>
        public override DataTable GetNoneData()
        {
            var request = new Request();
            request.Items.Add(new Condition
                    {
                        FieldName = "DWGL_ID", 
                        Operator = ConditionOperator.Equal, 
                        Values = new object[] {"null"}
                    });
            return GetData(request).Data;
        }

        public override void Add(DataTable data)
        {
            if (data.Rows.Count > 0)
            {
                const string sql = "insert into DWGLTABLE(DWGL_ID,DWGLCODE,DWNAME,DWFZR_ID,DWFZRNAME)" +
                                   "values(:DWGL_ID,:DWGLCODE,:DWNAME,:DWFZR_ID,:DWFZRNAME)";
                var parameters = new[]
                                     {
                                         MakeParam(":DWGL_ID", OracleType.VarChar, 36, "DWGL_ID"),
                                         MakeParam(":DWGLCODE", OracleType.VarChar, 10, "DWGLCODE"),
                                         MakeParam(":DWNAME", OracleType.VarChar, 80, "DWNAME"),
                                         MakeParam(":DWFZR_ID", OracleType.VarChar, 36, "DWFZR_ID"),
                                         MakeParam(":DWFZRNAME", OracleType.VarChar, 20, "DWFZRNAME")
                                     };
                OnAdd(data, sql, parameters);
            }
        }

        public override void Update(DataTable data)
        {
            if (data.Rows.Count > 0)
            {
                const string sql =
                    "update DWGLTABLE set " +
                    "DWGLCODE=:DWGLCODE," +
                    "DWNAME=:DWNAME," +
                    "DWFZR_ID=:DWFZR_ID," +
                    "DWFZRNAME=:DWFZRNAME where DWGL_ID =:DWGL_ID";
                var parameters = new[]
                                     {
                                         MakeParam(":DWGLCODE", OracleType.VarChar, 10, "DWGLCODE"),
                                         MakeParam(":DWNAME", OracleType.VarChar, 80, "DWNAME"),
                                         MakeParam(":DWFZR_ID", OracleType.VarChar, 36, "DWFZR_ID"),
                                         MakeParam(":DWFZRNAME", OracleType.VarChar, 20, "DWFZRNAME"),
                                         MakeParam(":DWGL_ID", OracleType.VarChar, 36, "DWGL_ID")
                                     };
                OnUpdate(data, sql, parameters);
            }
        }

        public override void Delete(List<string> ids)
        {
            const string sql = "delete from DWGLTABLE t where t.DWGL_ID in ('{0}')";
            ExecuteNoneQuery(string.Format(sql, string.Join("','", ids)));
        }
    }
}